(function(){
	window.Build=arguments.callee;
	var Me=Build;
	Me.params=[];
	Me.pkgHash={};
	Me.start=function(){
		var items=[];
		var queues=[""];
		var code=Shell.fs.readFileSync(Shell.path+"code/Main.js");
		while(queues.length>0){
			var queue=queues.shift();
			var arr=Shell.fs.readdirSync(Shell.path+"code/"+queue);
			for(var i=0;i<arr.length;i++){
				var file=arr[i];
				var filePath=Shell.path+"code/"+queue+file;
				var stat=Shell.fs.statSync(filePath);
				if(stat.isDirectory()){
					queues.push(queue+file+"/");
				}
				else if(file.slice(-3)==".js"){
					var name=file.slice(0,-3);
					var code=Shell.fs.readFileSync(filePath)+"";
					code=code.replace(/\r/g,"");
					code=code.replace(/(^|\n)\s*Class /,"$1class ");
					if(!RegExp(/\bclass\b/).test(code)) continue;
					var a=code.match(/\bclass\s+\w+\s+extends\s+(\w+)/);
					var parentName=!a?"":a[1];
					Me.pkgHash[name]=queue.replace(/\//g,".");
					items.push([name,parentName,code]);
				}
			}
		}
		Me.build(items);
	}
	Me.build=function(items){
		Me.classHash={};
		for(var i=0;i<items.length;i++){
			var arr=items[i];
			Me.classHash[arr[0]]=true;
			var baseIndex=-1;
			for(var j=i+1;j<items.length;j++){
				var arr2=items[j];
				if(arr[1]==arr2[0]){
					baseIndex=j;
					break;
				}
			}
			if(baseIndex>-1){
				items.splice(i,0,items[baseIndex]);
				items.splice(baseIndex+1,1);
				i--;
				continue;
			}
		}
		var code="";
		for(var i=0;i<items.length;i++){
			var arr=items[i];
			var code2=arr[2];
			code+=Me.formatCode(code2)+"\n";
		}
		code+="GAid.params="+JSON.stringify(Me.params);
		Shell.fs.writeFileSync(Shell.path+"js/dream-code.js",code);
	}
	Me.formatCode=function(code){
		code=code.replace(/\r/g,"");
		var arr=code.split("\n");
		var isDefineFunc=false;
		var className;
		var isData=false;
		for(var i=0;i<arr.length;i++){
			var str=arr[i];
			if(RegExp(/\$\w+.+\/\*/).test(str)){
				isData=true;
				arr[i]=Me.formatCodeFunc(str,className);
				continue;
			}
			if(isData){
				if(str.indexOf("*/}")==-1){
					arr[i]=str.slice(1);
				}
				else{
					isData=false;
				}
				continue;
			}
			str=str.replace(/^\s+/,"");
			str=str.replace(/\s+$/,"");
			str=str.replace(/;$/,"");
			str=str.replace(/(^\s*)(private|protected)\b/g,"$1public");
			if(className=="Main"&&!isDefineFunc&&str.indexOf("//[")==0){
				var s=str.slice(3).replace("]","");
				var a=s.split("=>");
				if(a[0]=="design"){
					s=Me.pkgHash[a[1]];
					a=s!=null?[a[0],s.replace(/\./g,"/")+a[1]+".js"]:null;
				}
				if(a) Me.params.push(a);
			}
			if(str.indexOf("//")==0) str="";
			str=" "+str;
			str=Me.formatNew(str);
			if(str.indexOf(" class ")==0){
				isDefineFunc=false;
				var a=str.split(/\s+/);
				className=a[2];
				str="(function(){\n";
				str+="var Me=Class.extends(\""+Build.pkgHash[className]+className+"\","+(a[4]||"null")+");";
				if(i>0){
					arr[0]=str;
					str="";
				}
			}
			else if(Me.getWordIndex(str," for(")>-1){
				str=str.replace(/\:\w+/g,"");
			}
			else if(Me.getWordIndex(str," var ")>-1){
				str=Me.formatCodeVar(str,isDefineFunc);
			}
			else if(Me.getWordIndex(str," function ")>-1){
				str=Me.formatCodeFunc(str,className);
				isDefineFunc=true;
			}
			else if(Me.getWordIndex(str,"function")>-1&&str.indexOf("{")>-1){
				isDefineFunc=true;
			}
			arr[i]=str;
		}
		code=arr.join("\n");
		code+="\n})();";
		return code;
	}
	Me.formatNew=function(rowStr){
		if(!RegExp(/\bnew /).test(rowStr)) return rowStr;
		var rowStr2="";
		for(var i=0;i<rowStr.length;i++){
			var s=rowStr.charAt(i);
			if(s=="\""||s=="'"){
				for(var j=i+1;j<rowStr.length;j++){
					var sj=rowStr.charAt(j);
					if(rowStr.charAt(j-1)!="\\"&&sj==s) break;
				}
				var str=rowStr.slice(i,j+1);
				str=str.replace(/\bnew\b/g,"%new%");
				rowStr2+=str;
				i=j;
			}
			else{
				rowStr2+=s;
			}
		}
		rowStr=rowStr2.replace(/\bnew\s+([A-Z]\w*)\(\s*./g,Me.formatNew2);
		rowStr=rowStr.replace(/%new%/g,"new");
		return rowStr;
	}
	Me.formatNew2=function(str,name){
		if(!Me.classHash[name]){
			return str;
		}
		var s=str.slice(-1);
		str="Class.new("+name;
		if(s!=")") str+=",";
		str+=s;
		return str;
	}
	Me.formatCodeFunc=function(str,className){
		var n=str.indexOf("(");
		var n2=str.indexOf("=");
		if(n2>-1&&n>n2) return str;
		var s1=str.slice(0,n);
		var s2=str.slice(n);
		var isStatic=s1.indexOf("static")>-1;
		str=(isStatic?"Me":"Me.proto")+".";
		s1=s1.replace(/\b(get|set)\s+(\w+)/g,"$1__$2");
		s1=s1.replace("public","").replace("static","").replace("function","").replace(/\s/g,"");
		s2=s2.replace(/\:\w+/g,"");
		var m=s2.match(/\w+=[\w\.\"\-#]+/g);
		s2=s2.replace(/=[\w\.\"\-#]+/g,"");
		for(var i=0;m&&i<m.length;i++){
			var s=m[i];
			s=s.replace(/(\w+)=(.+)/g,"if($1==null){$1=$2}");
			if(i>0) s2+=";";
			s2+=s;
		}
		if(!isStatic&&s1==className){
			s1="__ctor";
		}
		str+=s1+"=function"+s2;
		return str;
	}
	Me.formatCodeVar=function(str,isDefineFunc){
		var n=Me.getWordIndex(str," var ");
		if(!isDefineFunc&&n==0) str=" public "+str;
		if(Me.getWordIndex(str," static ")>-1){
			str="Me."+str.slice(str.indexOf(" var ")+5);
			if(str.indexOf("=")==-1) str+="=null";
		}
		else if(Me.getWordIndex(str," public ")>-1){
			str="Me.proto."+str.slice(str.indexOf(" var ")+5);
			if(str.indexOf("=")==-1) str+="=null";
		}
		var n2=Me.getWordIndex(str,":");
		if(n2>-1){
			var n3=Me.getWordIndex(str,"=");
			if(n3==-1){
				str=str.slice(0,n2);
			}
			else if(n3>n2){
				str=str.slice(0,n2)+str.slice(n3);
			}
		}
		return str;
	}
	Me.getWordIndex=function(str,s){
		var n=str.indexOf(s);
		var n2=str.indexOf("\"");
		if(n2>-1&&n>=n2) n=-1;
		return n;
	}
})();
Build.start();